import { get, post, postForm } from './request'
import type {
  DemoItem,
  PageResult,
  ReceptionItem,
  ReceptionPageParams,
  LeaseListItem,
  LeaseListParams,
  LoginTokenParams,
  LoginTokenData,
  UserDetail,
  BladeFile,
} from '../types/api'

export function fetchDemoList() {
  return get<DemoItem[]>('/demo/list')
}

// 后端为 envelope(code/success/data)，拦截器已统一解析，直接返回数据部分
export function fetchReceptionPage(params: ReceptionPageParams) {
  // GET 请求，参数只包含 current/size
  return get<PageResult<ReceptionItem>>('/property/reception/page', { params })
}

// 接房详情
export function fetchReceptionDetail(id: number | string) {
  return get<ReceptionItem>('/property/reception/detail', { params: { id } })
}

// 接房确认
export function confirmReception(data: any) {
  return post<any>('/property/reception/confirm', data)
}

// 合同核验
export function verifyContract(data: {
  id: number | string
  houseId: number | string
  contractVerification?: number
}) {
  return post<any>('/property/reception/confirm', data)
}

// 接房确认（仅 id）
export function confirmReceptionById(
  id: number | string,
  houseId: number | string,
  files?: string,
) {
  return post<any>('/property/reception/confirm', { id, houseId, files })
}

// 取消接房（仅 id）
export function cancelReceptionById(id: number | string, houseId: number | string) {
  return post<any>('/property/reception/cancel', { id, houseId })
}

// 取消接房（可携带 remark 等可选字段）
export function cancelReception(data: {
  id: number | string
  houseId: number | string
  remark?: string
  files?: string
}) {
  return post<any>('/property/reception/cancel', data)
}

// 完成房屋验收（申请退租第一步）
export function completeHouseInspection(id: number | string) {
  return post<any>('/property/lease/complete-house-inspection', { id, step: 1 })
}

// 完成房屋验收（提交表单版本）
export function completeLeaseInspection(data: {
  leaseId: number | string
  files?: string
  returnTime?: string
  isAllocate?: number
  reasonAllocate?: string
  state: number
}) {
  return post<any>('/property/lease/complete-house-inspection', data)
}

// 申请退租（房屋维度）
export function applyLeaseApplication(houseId: number | string, applicationId?: string) {
  return post<any>('/property/lease/application', { houseId, applicationId })
}

// 待退租列表分页
export function fetchPendingLeaseList(params: LeaseListParams) {
  return get<PageResult<LeaseListItem>>('/property/lease/pending-lease-list', { params })
}

// 已退租列表分页
export function fetchLeasedOutList(params: LeaseListParams) {
  return get<PageResult<LeaseListItem>>('/property/lease/leased-out-list', { params })
}

// 登录接口：/property/user-auth/token （query 方式）

// 统一由拦截器处理业务 envelope 与错误提示，这里不再暴露 envelope 类型

export function loginToken(params: LoginTokenParams) {
  // POST + params（追加到查询串），无请求体；成功直接返回 LoginTokenData
  return postForm<LoginTokenData>('property/user-auth/token', params)
}

// 用户详情（需要传入 id）
export function fetchUserDetail(id: number | string) {
  return get<UserDetail>('/property/user/detail', { params: { id } })
}

// OSS 上传文件
export function uploadFile(file: File) {
  const form = new FormData()
  form.append('file', file)
  return post<BladeFile>('blade-resource/oss/endpoint/put-file', form)
}
